const app = getApp();
const { get, post } = require('../../utils/request');
Page({
  data: {
    currentAddress: '河南省 < 郑州市 < 金水区',
    province:'广东省',
    city:'广州市',
    district:'海珠区',
    longitude:'',
    latitude:'',
    storeList: [
      {
        id: 1,
        fullName: '北京汽车郑州熙地港王府井体验店',
        salesAddress: '郑州市金水区文化路街道花园路王府井百货一楼'
      }
    ],
    region: ['广东省', '广州市', '海珠区'],
    lt: '<'
  },
  onLoad: function(options) {
    debugger
    // options就是传递过来的参数对象
    console.log(options.address); // 输出："123"（注意是字符串类型）
    if((options.address=="" || !options.address) && !options.province){
      options.province = "北京市"
      options.city = "北京市"
      options.district = "朝阳区"
      options.latitude = "39.960310245791396"
      options.longitude = "116.64395635228565"
    }
    if(options.address){
      const regex = /(?<province>[^省]+省|.+自治区|.+特别行政区|.+市)?(?<city>[^市]+市|.+自治州)?(?<district>[^区县]+区|.+县|.+旗|.+自治县|.+自治旗)?/;
      const match = options.address.match(regex);
      if (match) {
        const province = match.groups.province || '';
        const city = match.groups.city || '';
        const district = match.groups.district || '';
        let add = province + ' < ' + city
        if(district){
          add += ' < ' + district
        }
        this.setData({
          currentAddress: add,
          province: province,
          city: city,
          district: district
        });
      }
    } else {
      if(options.province){
        const province = options.province;
        const city = options.city;
        const district = options.district;
        let add = province + ' < ' + city
        if(district){
          add += ' < ' + district
        }
        this.setData({
          currentAddress: add,
          province: province,
          city: city,
          district: district
        });
      }
    }
    console.log(this.data.currentAddress)
    console.log(this.data.province)
    let addressArr = []
    addressArr.push(this.data.province)
    addressArr.push(this.data.city)
    addressArr.push(this.data.district)
    this.setData({
      region: addressArr,
      latitude: options.latitude,
      longitude: options.longitude
    })
    this.getStore();
    console.log('省:', this.data.province); // 输出: 省: 浙江省
    console.log('市:', this.data.city);     // 输出: 市: 杭州市
    console.log('区:', this.data.district); // 输出: 区: 西湖区
  },
  getStore(){
    // GET请求示例listByPosition
    get('/system/store/listByPosition', {
      province: this.data.province,
      city: this.data.city,
      county: this.data.district,
      latitude: this.data.latitude,
      longitude: this.data.longitude
    }).then(res => {
      debugger
      console.log('接口返回数据:', res.data.rows);
      if (res.data.code === 200) {
        this.setData({
          storeList: res.data.rows // 将 rows 数组存入 storeList
        });
      }
    }).catch(err => {
      console.error(err);
    });
  },

  onBack() {
    wx.navigateBack();
  },

  onChangeAddress() {
    // 这里可以调用选择地址的逻辑，比如弹出地址选择器或跳转到地址选择页面
    wx.navigateTo({
      url: '/pages/select-province/select-province'
    });
  },

  onStoreSelect(e) {
    const storeId = e.currentTarget.dataset.id;
    const selectedStore = this.data.storeList.find(store => store.id === storeId);
    // 这里可以将选择的门店信息返回给上一页
    wx.setStorageSync('selectedStore', selectedStore);
    debugger
    const pages = getCurrentPages();
    const prevPage = pages[pages.length - 2]; // 获取上一页实例
    if (prevPage) {
      prevPage.refreshData(); // 调用上一页的自定义刷新方法
    }
    wx.navigateBack();
  },
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    if(e.detail.value){
      this.setData({
        region: e.detail.value
      })
      this.setData({
        province: this.data.region[0],
        city: this.data.region[1],
        district: this.data.region[2]
      });
      this.getStore();
    }  
  }
});